package auto.test.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import auto.test.util.ExeclUtil;
import auto.test.util.GetConfigPath;
import auto.test.util.ProUtil;


public class BasePage {
	ExeclUtil exclutil=new ExeclUtil();

	GetConfigPath getconfig=new GetConfigPath();
	//	ProUtil ProU=new ProUtil(getconfig.GetelementPath());
		public WebDriver driver;
		static Logger logger=Logger.getLogger(BasePage.class);
		public BasePage(WebDriver driver) {
			this.driver=driver;
		}

	public BasePage() {

	}

	//显示等待查找元素方法
		public WebElement GetElement(String key){
			WebDriverWait wait = new WebDriverWait(driver,20);

		    WebElement Element = null;
			try {
				Element = wait.until(ExpectedConditions.presenceOfElementLocated(this.GetByLocal(key)));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return Element;
		}

		//获取元素集合
		public List<WebElement> GetElements(String key){
			List<WebElement> Element = null;
			try {
				Element = driver.findElements(this.GetByLocal(key));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return Element;
		}
		
		
		public List<WebElement> Getcontainstexts(String key) {

			List<WebElement> Element = null;
			try {
				Element = driver.findElements(By.xpath("//*[contains(text(),'"+exclutil.getvalue(key)+"')]"));
				System.out.println(exclutil.getvalue(key));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return Element;
			
		}
		
		public WebElement Getcontainstext(String key) {
       //     ExeclUtil exclutil=new ExeclUtil();

			WebElement Element = null;
			try {
				Element = driver.findElement(By.xpath("//*[contains(text(),'"+exclutil.getvalue(key)+"')]"));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return Element;
			
		}
		
		public WebElement Getcontainskey(String key) {
         //   ExeclUtil exclutil=new ExeclUtil();

			WebElement Element = null;
			try {
				Element = driver.findElement(By.xpath("//*[contains(text(),'"+key+"')]"));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return Element;
			
		}
		
	public WebElement Get_driver_xpath(String key){
		WebElement Element = null;
		Element=driver.findElement(By.xpath(key));
		return Element;
	}
		 /*	public List<WebElement> Getcontainsclassname(String key) {
           ExeclUtil exclutil=new ExeclUtil();

			List<WebElement> Element = null;
			try {
				Element = driver.findElements(By.className(exclutil.getvalue(key)));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return Element;
			
			  return GetElements(key);
			
		}
		
		*/
		
		public By GetByLocal(String key){

        //    ExeclUtil exclutil=new ExeclUtil();

            logger.info("你的定位信息的key为"+key);

            String LocatorBy = null;
			try {
				LocatorBy = exclutil.getLocatorBy(key);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

            String LocatorValue = null;
			try {
				LocatorValue = exclutil.getLocatorValue(key);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

            logger.info("你的定位信息的方式为"+LocatorBy);

            logger.info("你的定位信息的值为"+LocatorValue);

            if(LocatorBy.equals("id")){

            return    By.id(LocatorValue);

            }

            else if (LocatorBy.equals("name")){

                return    By.name(LocatorValue);

            }

            else if (LocatorBy.equals("className")){

                return    By.className(LocatorValue);

            }else if (LocatorBy.equals("linktext")){

                return    By.linkText(LocatorValue);

            }else if (LocatorBy.equals("css")){

                return    By.cssSelector(LocatorValue);

            }else if (LocatorBy.equals("tagName")){

                return    By.tagName(LocatorValue);

            }

            else if(LocatorBy.equals("xpath")){

                return    By.xpath(LocatorValue);

            }else {

                System.out.print("你选择的定位方式：["+LocatorBy+"] 不被支持!");

                return null;

        

            }

        }
		
		/*  //查找元素10次，直到找到为止
		public WebElement GetElement(String key){
			boolean flag=true;
			int i=0;
			WebElement Element = null;
			while (flag){
				try {
				 Element= driver.findElement(this.GetByLocal(key));
				 flag=false;
				}catch (Exception e){
					i=i+1;
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					if(i==10) {
						flag=false;
					}
				}	
			}
			return Element;
		}
		
		//通过配置文件中的key值获取对应定位方式和定位值
		public By GetByLocal(String key){
			ProUtil ProU=new ProUtil("element.properties");
			logger.info("你的定位信息的key为"+key);
			String Locator=ProU.GetPro(key);
			String LocatorBy=Locator.split(">")[0];
			String LocatorValue=Locator.split(">")[1];
			logger.info("你的定位信息的方式为"+LocatorBy);
			logger.info("你的定位信息的值为"+LocatorValue);
			if(LocatorBy.equals("id")){
			return	By.id(LocatorValue);
			}
			else if (LocatorBy.equals("name")){
				return	By.name(LocatorValue);
			}
			else if (LocatorBy.equals("className")){
				return	By.className(LocatorValue);
			}else if (LocatorBy.equals("linktext")){
				return	By.linkText(LocatorValue);
			}else if (LocatorBy.equals("css")){
				return	By.cssSelector(LocatorValue);
			}else if (LocatorBy.equals("tagname")){
				return	By.tagName(LocatorValue);
			}
			else if(LocatorBy.equals("xpath")){
				return	By.xpath(LocatorValue);
			}else {
				System.out.print("你选择的定位方式：["+LocatorBy+"] 不被支持!");
				return null;
		
			}
		}
*/
		
		
	

		
		
		public boolean GetCookie(String key) {
			boolean flag=false;
			Set<Cookie> cookies=driver.manage().getCookies();
			for(Cookie cookie:cookies) {
				 System.out.println("---->"+cookie);
				 if(cookie.getName().equals(key)) {
					 flag=true;
				 } 
			 }
			return flag;
		}



	
	}

